package com.ktc.recruit.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
import com.ktc.recruit.dao.EnterpriseDao;
import com.ktc.recruit.pojo.Enterprise;

/**
 * @Description 企业 服务层
 * @author admin
 * @date 2021-04-12 10:21:21
*/
@Service
public class EnterpriseService {

	@Autowired
	private EnterpriseDao enterpriseDao;

	@Autowired
	private IdWorker idWorker;

	/**
	* 查询全部列表
	* @return
	*/
	public List<Enterprise> findAll() {
		return enterpriseDao.findAll();
	}

	/**
	* 条件查询+分页
	* @param whereMap
	* @param page
	* @param size
	* @return
	*/
	public Page<Enterprise> findSearch(Map whereMap, int page, int size) {
		Specification<Enterprise> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return enterpriseDao.findAll(specification, pageRequest);
	}

	/**
 	* 条件查询
	* @param whereMap
	* @return
	*/
	public List<Enterprise> findSearch(Map whereMap) {
		Specification<Enterprise> specification = createSpecification(whereMap);
		return enterpriseDao.findAll(specification);
	}

	/**
	* 根据ID查询实体
	* @param id
	* @return
	*/
	public Enterprise findById(String id) {
		return enterpriseDao.findById(id).get();
	}

	/**
	* 增加
	* @param enterprise
	*/
	public void add(Enterprise enterprise) {
		enterprise.setId( idWorker.nextId()+"" );
		enterpriseDao.save(enterprise);
	}

	/**
	* 修改
	* @param enterprise
	*/
	public void update(Enterprise enterprise) {
		enterpriseDao.save(enterprise);
	}

	/**
	* 删除
	* @param id
	*/
	public void deleteById(String id) {
		enterpriseDao.deleteById(id);
	}

	/**
	* 动态条件构建
	* @param searchMap
	* @return
	*/
	private Specification<Enterprise> createSpecification(Map searchMap) {

		return new Specification<Enterprise>() {

			@Override
			public Predicate toPredicate(Root<Enterprise> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				//企业名称
				if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
					predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
				}
				//企业简介
				if (searchMap.get("summary")!=null && !"".equals(searchMap.get("summary"))) {
					predicateList.add(cb.like(root.get("summary").as(String.class), "%"+(String)searchMap.get("summary")+"%"));
				}
				//企业地址
				if (searchMap.get("address")!=null && !"".equals(searchMap.get("address"))) {
					predicateList.add(cb.like(root.get("address").as(String.class), "%"+(String)searchMap.get("address")+"%"));
				}
				//标签列表
				if (searchMap.get("labels")!=null && !"".equals(searchMap.get("labels"))) {
					predicateList.add(cb.like(root.get("labels").as(String.class), "%"+(String)searchMap.get("labels")+"%"));
				}
				//坐标
				if (searchMap.get("coordinate")!=null && !"".equals(searchMap.get("coordinate"))) {
					predicateList.add(cb.like(root.get("coordinate").as(String.class), "%"+(String)searchMap.get("coordinate")+"%"));
				}
				//是否热门
				if (searchMap.get("ishot")!=null && !"".equals(searchMap.get("ishot"))) {
					predicateList.add(cb.like(root.get("ishot").as(String.class), "%"+(String)searchMap.get("ishot")+"%"));
				}
				//LOGO
				if (searchMap.get("logo")!=null && !"".equals(searchMap.get("logo"))) {
					predicateList.add(cb.like(root.get("logo").as(String.class), "%"+(String)searchMap.get("logo")+"%"));
				}
				//职位数
				if (searchMap.get("jobcount")!=null && !"".equals(searchMap.get("jobcount"))) {
					predicateList.add(cb.like(root.get("jobcount").as(String.class), "%"+(String)searchMap.get("jobcount")+"%"));
				}
				//URL
				if (searchMap.get("url")!=null && !"".equals(searchMap.get("url"))) {
					predicateList.add(cb.like(root.get("url").as(String.class), "%"+(String)searchMap.get("url")+"%"));
				}

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
	}

	/**
	 * 查询热门企业
	 * @return
	 */
	public List<Enterprise> hotlist() {

		return enterpriseDao.findTop9ByIshot("1");
	}
}
